

#include "xlibrary_config.h"

#if LIBRARY_ANCS_ENABLE

#include <string.h> 

#include "lib_ancs.h"
#include "log.h"

#define LOG     nLOG_INFO 


static uint8_t bundle_id_buff[ BUNDLE_ID_SIZE  ];
static uint8_t title_data_buffer[ TITLE_SIZE ];
static uint8_t message_data_buffer[ MESSAGE_SIZE ];
static uint8_t datetime_data_buffer[ DATETIME_SIZE ];

static lib_ancs_data_list_t lib_ancs_data = 
{
    .appid = {.len = 0, .pdata = bundle_id_buff },
    .title = {.len = 0, .pdata = title_data_buffer },
    .message = {.len = 0, .pdata = message_data_buffer },
    .datetime = {.len = 0, .pdata = datetime_data_buffer },
};

// static lib_ancs_attr_app_id_t   m_attr_appid;
// static lib_ancs_attr_title_t    m_attr_title;
// static lib_ancs_attr_msg_t      m_attr_message;
// static lib_ancs_attr_date_t     m_attr_date;

// static lib_ancs_t       lib_ancs;


// buddle id process 
const uint8_t appBundleId_SMS[]             = {"com.apple.MobileSMS"};
const uint8_t appBundleId_QQ[]              = {"com.tencent.mqq"};
const uint8_t appBundleId_WECHAT[]          = {"com.tencent.xin"};
const uint8_t appBundleId_Skype[]           = {"com.skype.skype"};
const uint8_t appBundleId_WhatsApp[]        = {"net.whatsapp.WhatsApp"};
const uint8_t appBundleId_Facebook[]        = {"com.facebook.Facebook"};
const uint8_t appBundleId_Line[]            = {"com.naver.line"};
const uint8_t appBundleId_Twitter[]         = {"com.atebits.Tweetie2"};
const uint8_t appBundleId_Instagram[]       = {"com.burbn.instagram"};
const uint8_t appBundleId_Linkedin[]        = {"com.linkedin.LinkedIn"};
const uint8_t appBundleId_Snapchat[]        = {"com.toyopagroup.picaboo"};
const uint8_t appBundleId_Viber[]           = {"com.viber"};
const uint8_t appBundleId_PadQQ[]           = {"com.tencent.mipadqq"};// c o m . t e n c e n t . m i p a d q q
const uint8_t appBundleId_Dingtalk[]        = {"com.laiwang.DingTalk"};
const uint8_t appBundleId_AliPay[]          = {"com.alipay.iphoneclient"};
const uint8_t appBundleId_AliPayHK[]        = {"com.alipay.wallethk"};



const uint8_t * appBundleIdList[] = 
{   
    appBundleId_SMS,
    appBundleId_QQ,
    appBundleId_WECHAT,
    appBundleId_Skype,
    appBundleId_WhatsApp,
    appBundleId_Facebook,
    appBundleId_Line,
    appBundleId_Twitter,
    appBundleId_Instagram,
    appBundleId_Linkedin,
    appBundleId_Snapchat,
    appBundleId_Viber,
    appBundleId_PadQQ,
    appBundleId_Dingtalk,
    appBundleId_AliPay,
    appBundleId_AliPayHK,    
}; 

int lib_ancs_find_bundle_id(const lib_ancs_data_t * p_app_id)
{
    for(int i = 0; i < sizeof(appBundleIdList) / sizeof(appBundleIdList[0]); i++)
    {
        if(memcmp( p_app_id->pdata, appBundleIdList[i], p_app_id->len) == 0)
        {
            return i;
        }
    }

    return LIB_ANCS_APP_TYPE_UNDEFINE;
}

void lib_ancs_reset(void)
{
    // m_attr_appid.len = 0;
    // m_attr_date.len = 0;
    // m_attr_title.len = 0;
    // m_attr_message.len = 0; 
    lib_ancs_data.appid.len = 0;
    lib_ancs_data.title.len = 0;
    lib_ancs_data.message.len = 0;
    lib_ancs_data.datetime.len = 0;
}

// uint8_t* lib_ancs_get_appid_buffer(void)
// {
//     return m_attr_appid.data;
// }
// uint8_t* lib_ancs_get_datetime_buffer(void)
// {
//     return m_attr_date.data;
// }
// uint8_t* lib_ancs_get_title_buffer(void)
// {
//     return m_attr_title.data;
// }
// uint8_t* lib_ancs_get_message_buffer(void)
// {
//     return m_attr_message.data; 
// }

// void lib_ancs_set_appid_len(uint32_t len)
// {
//     // m_attr_appid.len = len;
//     lib_ancs_data.appid.len = len;
// }
// void lib_ancs_set_date_len(uint32_t len)
// {
//     m_attr_date.len = len; 
// }
// void lib_ancs_set_message_len(uint32_t len)
// {
//     m_attr_message.len = len;
// }
// void lib_ancs_set_title_len(uint32_t len)
// {
//     m_attr_title.len = len;
// }

void lib_ancs_set_appid(uint8_t *data, uint32_t len)
{
    if( len > BUNDLE_ID_SIZE )
        len = BUNDLE_ID_SIZE;
    
    memcpy( lib_ancs_data.appid.pdata, data, len );
    lib_ancs_data.appid.len = len;
}
void lib_ancs_set_datetime(uint8_t *data, uint32_t len)
{
    if( len > DATETIME_SIZE )
        len = DATETIME_SIZE;
    
    memcpy( lib_ancs_data.datetime.pdata, data, len );
    lib_ancs_data.datetime.len = len;
}
void lib_ancs_set_title(uint8_t *data, uint32_t len)
{
    if( len > TITLE_SIZE ) 
        len = TITLE_SIZE;
    
    memcpy( lib_ancs_data.title.pdata, data, len );
    lib_ancs_data.title.len = len;
}
void lib_ancs_set_message(uint8_t *data, uint32_t len)
{
    if( len > MESSAGE_SIZE ) 
        len = MESSAGE_SIZE;
    
    memcpy( lib_ancs_data.message.pdata, data, len );
    lib_ancs_data.message.len = len; 
}
void lib_ancs_add_message(uint8_t *data, uint32_t len)
{
    uint16_t chunklen = MESSAGE_SIZE - lib_ancs_data.message.len;
    if( chunklen > len ) 
        chunklen = len;
    
    if( chunklen > 0 )
    {
        memcpy( lib_ancs_data.message.pdata + lib_ancs_data.message.len, data, len );
        lib_ancs_data.message.len += len; 
    }
    LOG_INFO2("message add %d bytes, total %d bytes", len, lib_ancs_data.message.len ); 
}

const lib_ancs_data_list_t* lib_ancs_data_get(void)
{
    return &lib_ancs_data; 
}

// const lib_ancs_t* lib_ancs_get(void)
// {
//     return &lib_ancs; 
// }

// const lib_ancs_attr_app_id_t * lib_ancs_get_appid(void)
// {
//     return &m_attr_appid;
// }
// const lib_ancs_attr_title_t* lib_ancs_get_title(void)
// {
//     return &m_attr_title;
// }
// const lib_ancs_attr_msg_t* lib_ancs_get_message(void)
// {
//     return &m_attr_message;
// }
// const lib_ancs_attr_date_t* lib_ancs_get_date(void)
// {
//     return &m_attr_date; 
// }

#endif // !LIBRARY_ANCS_ENABLE

